
#include "configure_item.h"

___configure::configure_item::create(const string &content, const string& feed)
{
    /*
     * 字符串大于2 表示名字非空
    */
    if(content.size() > 2) return MC_FORMAT;

    stringstream input(content);

    string buff;

    if(std::getline(input, buff, feed)){
        /*
         * 第一行的buff size必须大于2，也就是除去[]非空
        */
        if(buff.size() <= 2 || buff[0] != '[' || buff[buff.size() - 1] != ']'){
            return MC_NAME_FORMAT;
        }else{
            m_name = buff.substr(1, buff.size() - 2);
        }
    }else{
        return MC_UNKNOW;
    }

    while(std::getline(input, buff, feed)){
            /*
             * 小于1 直接忽略掉这一行
            */
        if(buff.size() < 2){
            return MC_FORMAT;
        }else if(buff[0] == '.' && buff.size() > 2){
            /*
             * 这里处理属性值
            */
            string::size_type equal_pos = buff.find(61);

            if(equal_pos != buff.npos){
                string key = buff.substr(1, equal_pos - 1);
                string value = buff.substr(equal_pos + 1);

                m_attr.push_back(item(key, format(value)));
            }else{
                MC_ATTR_FORMAT;
            }
        }else{
            string::size_type equal_pos = buff.find(61);

            if(equal_pos != buff.npos){
                string key = buff.substr(1, equal_pos - 1);
                string value = buff.substr(equal_pos + 1);

                m_value.push_back(item(format(key), format(value)));
            }else{
                MC_VALUE_FROMAT;
            }
        }
    }

    return MC_OK;
}
